/**
 * 
 */
package com.audi.box.server.handler;

import java.util.ArrayList;

import android.util.Log;

import com.audi.box.controller.DeviceController;
import com.audi.box.controller.IController;
import com.audi.box.server.message.HandlerException;
import com.audi.box.server.message.ObjectCombine;
import com.audi.dto.DeviceItem;
import com.audi.dto.PlaceItem;

/**
 * @author HuyPVA
 * 
 */
public class DeviceHandler extends Handler {
	private static DeviceHandler deviceHandle;
	private static Object deviceObj = new Object();

	/**
	 * @param controller
	 */
	private DeviceHandler(IController controller) {
		super(controller);
	}

	public static DeviceHandler getInstance(IController controller) {
		if (deviceHandle == null) {
			synchronized (deviceObj) {
				if (deviceHandle == null) {
					deviceHandle = new DeviceHandler(controller);
				}
				return deviceHandle;
			}
		} else {
			return deviceHandle;
		}
	}

	/*
	 * Get DeviceController
	 */
	public DeviceController getDeviceController() {
		return (DeviceController) getController();
	}

	/*
	 * Process message by action
	 */
	@Override
	public Object process(int actionId, String data) throws HandlerException {
		Object result = null;
		DeviceItem deviceItem = null;

		switch (actionId) {
		case ADD_DEVICE:
			deviceItem = getGson().fromJson(data, DeviceItem.class);
			result = getDeviceController().addDevice(deviceItem);
			break;
		case GET_LIST_DEVICE_BY_GROUP:
			int groupType = getGson().fromJson(data, Integer.class);
			result = getDeviceController().getDeviceList(groupType);
			break;
		case GET_ALL_DEVICE:
			result = getDeviceController().getDeviceList();
			break;
		case GET_DEVICE_LIST_BY_USER:
			int userId = getGson().fromJson(data, Integer.class);
			result = getDeviceController().getDeviceByUserId(userId);
			break;
		case GET_DEVICE_LIST_BY_PLACE:
			int placeId = getGson().fromJson(data, Integer.class);
			result = getDeviceController().getDeviceByPlaceId(placeId);
			break;
		case GET_DEVICE_BY_ID:
			int deviceId = getGson().fromJson(data, Integer.class);
			result = getDeviceController().getDeviceById(deviceId);
			break;
		case CHECK_DEVICE_EXIST:
			deviceItem = getGson().fromJson(data, DeviceItem.class);
			result = getDeviceController().isExist(deviceItem);
			break;
		case DELETE_DEVICE:
			deviceItem = getGson().fromJson(data, DeviceItem.class);
			result = getDeviceController().deleteDevice(deviceItem);
			break;
		case RENAME_DEVICE:
			deviceItem = getGson().fromJson(data, DeviceItem.class);
			result = getDeviceController().renameDevice(deviceItem);
			break;
		case UPDATE_DEVICE:
			deviceItem = getGson().fromJson(data, DeviceItem.class);
			result = getDeviceController().updateDevice(deviceItem);
			break;
		case CHANGE_PLACE_OF_DEVICE:
			deviceItem = getGson().fromJson(data, DeviceItem.class);
			result = getDeviceController().changePlace(deviceItem);
			break;
		case CHANGE_PLACE_OF_DEVICE_LIST:
			ObjectCombine objectCombine = getGson().fromJson(data, ObjectCombine.class);
			String strObject1 = objectCombine.getObj1();
			String strObject2 = objectCombine.getObj2();
			ArrayList<DeviceItem> lstDevice = (ArrayList<DeviceItem>) convertJson2List(strObject1, DeviceItem.class);
			PlaceItem placeItem = getGson().fromJson(strObject2, PlaceItem.class);
			result = getDeviceController().changePlaceforDeviceList(lstDevice, placeItem);
			break;
		case GET_LIST_DEVICE_FAVORITE:			
			result = getDeviceController().getListDeviceFavorite();
			break;
		case UPDATE_FAVORITE_DEVICE:
			ArrayList<DeviceItem> deviceList = (ArrayList<DeviceItem>) convertJson2List(data, DeviceItem.class);			
			result = getDeviceController().changeFavoriteforDeviceList(deviceList);
			break;			
		default:
			Log.e("AudiBox", "[DeviceHandler.java] Function not found" + actionId);
			throw new HandlerException("Function " + actionId +" not found!!!");
		}
		return result;
	}

}
